// Copyright (C) 2008 Taylor L. Riche <riche@cs.utexas.edu>
//  
// This program is free software; you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation; either version 2 of the License, or
// (at your option) any later version.
// 
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
// 
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software 
// Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
//

#include <sys/stat.h>
#include <fcntl.h>
#include "lc_validate_op_Operator.h"
#include <hashlibpp.h>
#define LAGNIAPE_ROOT_DIR "/home/riche/"
#define BUFFER_SIZE 8192


void 
validate_op::validateRequest(requestTypes::NetRequest * data, unsigned long dataSize)
{
  assert(data != NULL);
  assert(data->getSocketID()>= 0);
  
  requestTypes::NetRequest * out_data = new requestTypes::NetRequest(NULL,0, data->getSocketID(), data->getFileName());
  if(data->getClose()) out_data->setClose(true);
  assert(out_data->getSocketID()>= 0);
  
  lagniappe::FlowID fid = out_data->getSourceID();
  //bool suspicious = false;

  if(!flowStateMap[fid]) {
    state_records::FlowRecord_p fr(new state_records::FlowRecord(getNewMutex()));
    flowStateMap[fid] = fr;
  }
//   else if(flowStateMap[fid]->getState() == state_records::MAYBE) {
//     suspicious = true;
//   }
  else if(flowStateMap[fid]->getState() == state_records::DROP) {
    delete out_data;
    out_data = NULL;
  }

//       std::string teststring;
//       for(int i = 0; i < 5000; ++i) {
//         teststring += 'a';
//       }
//     sha512wrapper hasher;
//     //boost::hash<std::string> string_hash;
//     std::string hash;
//     for(int i = 0; i < 600; i++) {
//       hash = hasher.getHashFromString(boost::lexical_cast<std::string>(i));
//     }

  if(out_data != NULL) {
    //    if(!suspicious) {
      valid_requests(out_data, sizeof(requestTypes::NetRequest));
      //}
      //else {
      //suspicious_requests(out_data, sizeof(requestTypes::NetRequest));
      //}
  }
  
  // the following clause is a hack
  if(!inProf) {
    //delete[] data->getBuffer();
    delete data;
  }
  
  return;
  
}

requestTypes::NetRequest *
validate_op::geninRequest()
{
  std::string request = "GET /index_small01.html HTTP/1.1";
  request += "\r\n\r\n";
  char * request_str = new char[request.length()];
  strcpy(request_str, request.c_str());
  //std::cerr << request_str << ":" << strlen(request_str) << ":" << request.length() << std::endl;
  return new requestTypes::NetRequest(request_str,strlen(request_str),0);

}

void
validate_op::newControlMessage(requestTypes::ControlMessage * data, unsigned long dataSize)
{
  lagniappe::FlowID fid = data->getFlowID();
  assert(flowStateMap[fid] != NULL);
  if(data->getType() == requestTypes::SUSPICIOUS) {
    flowStateMap[fid]->setState(state_records::MAYBE);
  }
  else if(data->getType() == requestTypes::GUILTY) {
    assert(flowStateMap[fid]->getState() == state_records::MAYBE);
    flowStateMap[fid]->setState(state_records::DROP);
    if(inProf) {
      flowStateMap[fid]->setState(state_records::MAYBE);
    }
  }
  else if(data->getType() == requestTypes::INNOCENT) {
    flowStateMap[fid]->setState(state_records::GOOD);
  }
  else {
    assert(false);
  }
  return;
}

requestTypes::ControlMessage *
validate_op::gencontrolRequest()
{
  lagniappe::FlowID fid = 0;
  state_records::FlowRecord_p fr(new state_records::FlowRecord(getNewMutex()));
  fr->setState(state_records::MAYBE);
  flowStateMap[fid] = fr;
  return new requestTypes::ControlMessage(requestTypes::GUILTY, 0);
}

void
validate_op::my_install(void * data)
{
}

void *
validate_op::my_get()
{
  return NULL;
}

void
validate_op::my_purge(void * data)
{
}

void
validate_op::my_init()
{
}
